﻿using System;
using System.Data.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Security.AccessControl;
using jSLCore.Service;
using jSLCore.Service.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Win32;
using jSLCore.VSPackageRegistering;

namespace UnitTests
{
    /// <summary>
    /// Summary description for MainTests
    /// </summary>
    [TestClass]
    public class MainTests
    {
        public MainTests()
        {
            
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in A class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

       /* [TestMethod]
        public void Utility_SetProperty_Test()
        {
            var type1 = new TestType1();

            type1.A = 3;
            type1.Child = new TestType1() {A = 5};

            type1.SetProperty("A", 6);

            Console.Out.WriteLine(type1.A);
        }

        [TestMethod]
        public void Utility_HasProperty_Test()
        {
            var type1 = new TestType1();

            type1.A = 3;
            type1.Child = new TestType1() { A = 5 };

            Console.Out.WriteLine(type1.HasProperty("A"));
        }
*/
        public class TestType1
        {
            public int A { get; set; }
            public TestType1 Child { get; set; }
        }

      /*  [TestMethod]
        public void Utility_GetEnumInfos()
        {
            var type1 = typeof (myenum);

            Console.Out.WriteLine(type1.Name);

            foreach (var val in Enum.GetValues(type1))
            {
                Console.Out.WriteLine("Value: {0} = {1}",val,(short)val);
            }
        }
*/
       /* [TestMethod]
        public void SetBitmapTest()
        {
            using (var db = new DB(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Users\jinek\Documents\Visual Studio 2008\Проекты\jSLCore\TestSLClient.Web\App_Data\NORTHWND.MDF;Integrated Security=True;User Instance=True"))
            {
                var bitmap = new Bitmap(@"c:\pic.jpg");
                var memoryStream = new MemoryStream();
                bitmap.Save(memoryStream,ImageFormat.Png);

                var bytes = memoryStream.ToArray();

                foreach (var employee in db.Employees)
                {
                    employee.Photo = new Binary(bytes);
                }
                
                db.SubmitChanges();
            }
        }*/

        [TestMethod]
        public void TestCreateTestUser()
        {
            using (var db = new SecurityDatabaseDataContext(@"Data Source=.\sqlexpress;AttachDbFilename=E:\Users\jinek\Documents\Visual Studio 2008\Проекты\TechnoCommunity\RussianUndegroundTechnoCommunity\jSLApp.Web\App_Data\UTechnoCommunity.mdf;Integrated Security=True;User Instance=True"))
            {
                var user = new User
                               {
                                   Login = "testuser",
                                   IsEnabled = true,
                                   CreationDate = DateTime.Now,
                                   Password = new Binary(jSLAuthorizationServiceBase.GetPasswordHash("qwerty"))
                               };
                db.Users.InsertOnSubmit(user);
                db.SubmitChanges();
            }
        }

        [TestMethod]
        public void TestInstall()
        {
            AssenblyInstaller.Install(@"r:\Temp");
            AssenblyInstaller.Uninstall();
        }

        [TestMethod]
        public void TestRegistry()
        {
            var softwareFolder =
                Registry.LocalMachine.OpenSubKey("SOFTWARE");

            if (softwareFolder == null) throw new InvalidOperationException(@"Не найдено: HKEY_LOCAL_MACHINE\SOFTWARE");

            var exFolders = softwareFolder.FindSubKey("AssemblyFoldersEx");
        }

        [TestMethod]
        public void TestRegister()
        {/*
            var assemblyFolder = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey(".NETFramework").OpenSubKey("AssemblyFolders",RegistryKeyPermissionCheck.ReadWriteSubTree,RegistryRights.FullControl);

            var registryKey = assemblyFolder.CreateSubKey("jSLCore");
            
            registryKey.SetValue("test", assemblyFolder, RegistryValueKind.String);*/

            {
                var msFolder = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft");

                if (System.Environment.Is64BitOperatingSystem)
                {
                    msFolder = msFolder.OpenSubKey("Wow6432Node");
                }

                var targetFolrder =
                    msFolder.OpenSubKey("Microsoft SDKs").OpenSubKey("Silverlight").OpenSubKey(
                        "v4.0").OpenSubKey("AssemblyFoldersEx",
                                                                                     RegistryKeyPermissionCheck.
                                                                                         ReadWriteSubTree,
                                                                                     RegistryRights.FullControl);
                var registryKey = targetFolrder.CreateSubKey("jSLCore");
                registryKey.SetValue(string.Empty, "test", RegistryValueKind.String);
            }
        }
    }
}
